package com.aiti.lulian.service.impl;

import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.gonxin.BankDto;
import com.aiti.lulian.entity.Bank;
import com.aiti.lulian.entity.Fund;
import com.aiti.lulian.mapper.gongxin.BankMapper;
import com.aiti.lulian.mapper.gongxin.FundMapper;
import com.aiti.lulian.service.IBankService;
import com.aiti.lulian.vo.gongxin.BankVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BankServiceImpl extends BaseSerivceImpl<BankMapper, Bank> implements IBankService {

    @Autowired
    private BankMapper bankMapper;

    @Autowired
    private FundMapper fundMapper;

    @Override
    public void addBank(BankDto dto) {
        Bank bank = new Bank();
        BeanUtils.copyProperties(dto, bank);
        bank.setBaseCreateTime(new Date());
        bank.setBaseUpdateTime(new Date());
        bankMapper.insert(bank);
    }

    @Override
    public List<BankVo> getBankList(Page<BankVo> page, BankDto dto) {
        return bankMapper.selectBankList(page, dto);
    }

    @Override
    public Message deleteBank(BankDto dto) {
        List<Fund> funds = fundMapper.selectList(new LambdaQueryWrapper<Fund>().eq(Fund::getBankId, dto.getBaseId()));
        if (funds.size() > 0) {
            return Message.fail("该银行存在资金，删除失败");
        }
        bankMapper.deleteById(dto.getBaseId());
        return Message.success("删除成功");
    }

    @Override
    public BankVo getBankDetails(String baseId) {
        return bankMapper.selectBankDetails(baseId);
    }

    @Override
    public void updateBank(BankDto dto) {
        Bank bank = new Bank();
        BeanUtils.copyProperties(dto, bank);
        bank.setBaseUpdateTime(new Date());
        bankMapper.updateById(bank);
    }
}
